#-*- coding: utf-8 -*-
# 测试问题1 conds中的value无法在question中标注出来
import editdistance
import jieba
import json
import matplotlib.pyplot as plt

filename = "../../data/train_deal_data.json"
num = 0

def most_similar(w, wlist):
    """从词表中找最相近的词（当无法全匹配的时候）
    """
    if len(wlist) == 0:
        return w
    scores = [editdistance.eval(w, t) for t in wlist]
    return wlist[scores.index(min(scores))]


def most_similar_2(word, sentence):
    """从句子s中找与w最相近的片段，
    借助分词工具和ngram的方式尽量精确地确定边界。
    """
    sw = jieba.lcut(sentence)
    sl = list(sw)
    sl.extend([''.join(i) for i in zip(sw, sw[1:])])
    sl.extend([''.join(i) for i in zip(sw, sw[1:], sw[2:])])
    sl.extend([''.join(i) for i in zip(sw, sw[1:], sw[2:], sw[3:])])
    return most_similar(word, sl)

# 找到conds中value_op一致，但是cel不一致的样本个数,有2949个
def find_for_sql(filename):
    global num
    with open(filename, 'r', encoding='utf-8') as lines:
        for line in lines:
            text = json.loads(line)
            question = text.get("question")
            sql = text.get("sql")
            conds = sql.get('conds')
            conds_value_op = []
            for i in range(len(conds)):
                conds_value_op.append(tuple(conds[i][1:]))
            if len(conds_value_op) != 1:
                conds_value_op = set(conds_value_op)
                if len(conds_value_op) == 1:
                    num += 1
    return num
# 绘制的bar图
height = [2949/41522, (1 - (2949/41522))]
x = ["1 value_op N cond_cel", "other"]
plt.bar(x, height, width=0.25)
for x, y in zip(x, height):
    plt.text(x, y,  y, ha='center', va='bottom')
plt.show()


# max_num = 0
# def find_max_conds_num_in_sql(filename):
#     global max_num
#     with open(filename, 'r', encoding='utf-8') as lines:
#         for line in lines:
#             text = json.loads(line)
#             sql = text.get("sql")
#             conds = sql.get('conds')
#             max = len(conds)
#             if max > max_num:
#                 max_num = max
#     return max_num

# print(find_max_conds_num_in_sql(filename))